<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="50%" destroy-on-close @closed="$emit('closed')" :align-center="true" :close-on-click-modal="false">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="right" style="height:550px;overflow:auto">
                    <div class="sketch_content">
                        <el-row :gutter="1">
                            <el-col :span="12">
                                <el-form-item label="针种" prop="needle_seed">
                                    <el-select v-model="form.needle_seed" filterable placeholder="请选择针种" style="width:100%" multiple>
                                        <el-option v-for="item in needleList" :key="item.id" :label="item.title" :value="item.id" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="织机时间" prop="loom_time">
                                    <div style="display:flex">
                                        <el-input v-model="form.loom_time" :value-format="'YYYY-MM-DD'" placeholder="请输入织机时间" clearable autocomplete="off"></el-input>
                                        <el-button text>分钟</el-button>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-col :span="24">
                            <el-form-item label="工序信息" prop="process_data">
                                <sc-form-table ref="table" v-model="form.process_data" :addTemplate="process" placeholder="暂无数据" hideAdd showSummary>
                                    <el-table-column label="工序" align="center">
                                        <template #default="scope">
                                            <div>
                                                {{scope.row.process_title}}
                                            </div>
                                        </template>
                                    </el-table-column>
                                    <!-- <el-table-column prop="GongxuSon" label="二级工序" align="center">
                                        <template #default="scope">
                                            <div>
                                                {{scope.row.two_process_title}}
                                            </div>
                                        </template>
                                    </el-table-column> -->
                                    <el-table-column prop="price" label="报工单价" align="center">
                                        <template #default="scope">
                                            <el-input v-model="scope.row.price" placeholder="请输入报工单价"></el-input>
                                        </template>
                                    </el-table-column>
                                </sc-form-table>
                            </el-form-item>
                        </el-col>
                    </div>
                </el-form>
            </el-main>
            <el-footer>
                <div style="display: flex;justify-content: end;">
                    <el-button @click="visible=false" >取 消</el-button>
                    <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">完 成</el-button>
                </div>
            </el-footer>
        </el-container>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '添加客户',
					edit: '编辑客户',
					over: '完成打板'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
                    needle_seed:"",
                    loom_time:"",
                    process_data:[]
				},
				//验证规则
				rules: {
                    needle_seed: [
						{required: true, message: '请选择'}
					],
                    loom_time: [
						{required: true, message: '请输入织机时间'}
					],
				},
                process:{process_id:"",price:""},
                needleList:[],//针种
			}
		},
		mounted() {
            this.getNeedle()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.develop.edition.complete.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			async setData(data){
                this.form.id = data.id
                var res = await this.$API.develop.edition.detail.get({id:data.id});
                this.form.needle_seed = res.data.goods.needle_seed
                // this.form.loom_time = res.data.goods.loom_time
                this.form.loom_time = res.data.loom_time
                let process = [
                    {process_id:1,process_title:"毛料"},
                    {process_id:2,process_title:"织机"},
                    {process_id:3,process_title:"缝盘"},
                    {process_id:4,process_title:"后整"},
                    {process_id:5,process_title:"二次工艺"},
                    {process_id:6,process_title:"利润"},
                ]
                let a = []
                process.map(item=>{
                    a.push({
                        id:item.id,
                        process_id:item.process_id,
                        process_title:item.process_title,
                        price:item.price,
                    })
                })
                this.form.process_data = a
                // res.data.process.map(item=>{
                //     a.push({
                //         id:item.id,
                //         process_id:item.process_id,
                //         process_title:item.process_title,
                //         two_process_title:item.two_process_title,
                //         price:item.price,
                //     })
                // })
			},
            //所有针种
            async getNeedle(){
                var res = await this.$API.all.needle.get();
                this.needleList = res.data;
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
